package cn.xie.blog.commonutil.response;

import cn.xie.blog.model.common.enums.response.ResponseCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author: xiaoxie
 * create: 2025-04-21 21:46
 * @BelongsProject: blog-parent
 * @BelongsPackage: cn.xie.blog.commonutil.response
 * description: 全局统一返回类
 */
@Data
@AllArgsConstructor
public class Response<T> {
    private int code;
    private String msg;
    private T data;
    public Response() {};

    // 返回数据
    public static <T> Response<T> build(T data) {
        Response<T> result = new  Response<T>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    // 手动实现 setData 方法
    public void setData(T data) {
        this.data = data;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public static <T>  Response<T> build(T data, Integer code, String message) {
        Response<T> result = build(data);
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    public static <T>  Response<T> build(T data, ResponseCodeEnum resultCodeEnum) {
        Response<T> result = build(data);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMessage());
        return result;
    }

    public static<T> Response<T> ok(){
        return Response.ok(null);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> Response<T> ok(T data){
        return build(data, ResponseCodeEnum.SUCCESS);
    }

    /**
     *  无返回数据
     * @param
     * @param <T>
     * @return
     **/
    public static<T> Response<T> fail(ResponseCodeEnum resultCodeEnum){
        return Response.fail(null, resultCodeEnum);
    }
    public static<T> Response<T> error(){
        return Response.build(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Response<T> fail(T data,ResponseCodeEnum resultCodeEnum){
        return build(data, resultCodeEnum);
    }

    public Response<T> message(String msg){
        this.setMsg(msg);
        return this;
    }

    public Response<T> code(Integer code){
        this.setCode(code);
        return this;
    }
    public static Response<?> error(Integer code, String msg) {
       return new Response<>(code, msg,null);
    }
}
